package com.springboot.springboot_demo.utils;


import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;


import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Title: PinyinUtil.class <br>
 * Description: pinyin4j 生僻字有bug  比如 吴潈霖 <br>
 * Create DateTime:2022/8/2 18:14 <br>
 *
 * @author peichang.yang
 */
public class PinyinUtil {
    private static PinyinUtil instance;

    public static PinyinUtil getInstance() {
        if (instance == null) {
            instance = new PinyinUtil();
        }
        return instance;
    }

    private HanyuPinyinOutputFormat outputFormat = null;

    private HanyuPinyinOutputFormat getOutputFormat() {
        if (outputFormat == null) {
            outputFormat = new HanyuPinyinOutputFormat();
            outputFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
            outputFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        }
        return outputFormat;
    }

    //获取字符串中所有字符首字母
    public String getStringInitial(String str) {
        StringBuffer sbf = new StringBuffer();
        if (str != null) {
            for (int i = 0; i < str.length(); i++) {
                sbf.append(getCharInitial(str.charAt(i)));
            }
        }
        return sbf.toString();
    }

    //获取中文字符首字母
    public String getCharInitial(char c) {
        try {
            String[] strs = PinyinHelper.toHanyuPinyinStringArray(c,
                    getOutputFormat());
            String initial = "";
            if (strs != null && strs.length > 0) {
                String str = strs[0];
                if (str != null && str.length() > 0) {
                    initial += str.charAt(0);
                }
            }
            return initial;
        } catch (Exception ex) {
            ex.printStackTrace();
            return c + "";
        }
    }

    //获取中文字符串全拼
    public String getStringPinyin(String chines) {
        char[] nameChar = chines.toCharArray();
        String pinyinStr = "";
        for (int i = 0; i < nameChar.length; i++) {
            try {
                char cha = nameChar[i];
                if (nameChar[i] > 128) {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(cha,
                            getOutputFormat())[0];
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                pinyinStr += nameChar[i];
            }
        }
        return pinyinStr;
    }

    public static void main(String[] args) throws IOException {
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            String str = "";
            fis = new FileInputStream("resource/username.txt");// FileInputStream


            // 从文件系统中的某个文件中获取字节
            isr = new InputStreamReader(fis);// InputStreamReader 是字节流通向字符流的桥梁,
            br = new BufferedReader(isr);// 从字符输入流中读取文件中的内容
            int index = 0;
            List<String> nameList = new ArrayList<String>();


            while ((str = br.readLine()) != null) {
                String tmp = "";
                if (str.length() == 2) {
                    tmp = PinyinUtil.getInstance().getStringPinyin(str);
                } else if (str.length() == 3) {
                    tmp = PinyinUtil.getInstance().getStringPinyin(
                            str.substring(0, 1));
                    tmp += PinyinUtil.getInstance().getStringInitial(
                            str.substring(1, 3));
                }
                index++;
                if (nameList.contains(tmp)
                        && Collections.frequency(nameList, tmp) >= 1) {
                    System.out.println(tmp
                            + Collections.frequency(nameList, tmp));
                    nameList.add(tmp);
                } else {
                    nameList.add(tmp);
                    System.out.println(tmp);
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("找不到指定文件");
        } catch (IOException e) {
            System.out.println("读取文件失败");
        } finally {
            br.close();
            isr.close();
            fis.close();
        }
    }

}
